﻿using System;
using System.Collections.Generic;
using System.Linq;
using Basic4Net.Entities;
using Microsoft.Data.Extensions;

namespace Basic4Net.BusinessRules
{
    public class PositionRelation:BaseBR
    {
        public bool UpdateTeacherPosition(List<int> teacherIds, int positionId, string schoolMark, ref string msg)
        {
            DBEntities db;
            using (db = new DBEntities())
            {
                
                foreach (var teacherId in teacherIds)
                {
                    var positonr = db.t_PositionRelation.FirstOrDefault(p => p.TeacherID == teacherId);

                    if (positonr == null)
                        db.AddTot_PositionRelation(new t_PositionRelation() { TeacherID = teacherId, PositionID = positionId, SchoolMark = schoolMark });
                    else
                    {
                        positonr.PositionID = positionId;
                        db.t_PositionRelation.ApplyCurrentValues(positonr);
                    }
                }
                try
                {
                    db.SaveChanges();
                }
                catch (Exception)
                {
                    msg = "设置失败!";
                    return false;
                }
                
                return true;
            }
        }

        /// <summary>
        /// 获取所有职务职称信息
        /// </summary>
        /// <returns></returns>
        public IList<object> GetTeacherByPosition(int startAt, int step, int? positionId, String teacherCode, ref int count)
        {
            DBEntities db;
            using (db = new DBEntities())
            {
                var postList = (from pr in db.t_PostRelation
                                join p in db.t_Post on pr.PostID equals p.RID into temp
                                from t in temp.DefaultIfEmpty()
                                select new { 
                                    TeacherId =  pr.TeacherID,
                                    PostId = t.RID == null ? -1 : t.RID,
                                    PostName = t.PostName == null ? "" : t.PostName
                                });

                var posList = (from pos in db.t_Position
                               select pos);

                var list = (from teacher in db.TEACHERINFO
                            join position in db.t_PositionRelation on teacher.RID equals position.TeacherID
                            into temp
                            from t in temp.DefaultIfEmpty()
                            where (positionId.HasValue ? (t.PositionID == positionId) : true)
                            && (String.IsNullOrEmpty(teacherCode) ? true : (teacher.TEACHCODE.Contains(teacherCode) || teacher.TEACHNAME.Contains(teacherCode)))
                            && teacher.TEACHCODE != "admin"
                            //orderby teacher.TEACHCODE descending
                            orderby teacher.TEACHCODE
                            select new
                            {
                                PositionId = t.PositionID,
                                PositionName = String.IsNullOrEmpty(posList.FirstOrDefault(p => p.RID == t.PositionID).FName) ? "" : posList.FirstOrDefault(p => p.RID == t.PositionID).FName,
                                TeacherId = teacher.RID,
                                TeacherCode = teacher.TEACHCODE,
                                TeacherName = teacher.TEACHNAME,
                                TeacherSex = teacher.SEXY,
                                PostId = postList.FirstOrDefault(p => p.TeacherId == teacher.RID).PostId == null ? -1 : postList.FirstOrDefault(p => p.TeacherId == teacher.RID).PostId,
                                PostName = String.IsNullOrEmpty(postList.FirstOrDefault(p => p.TeacherId == teacher.RID).PostName) ? "" : postList.FirstOrDefault(p => p.TeacherId == teacher.RID).PostName,
                                TeacherBirth = teacher.BIRTHDAY
                            }
                            );
                count = list.Count();
                IList<object> resList = list.Skip(startAt).Take(step).ToList<object>();

                return resList;
            }
        }
    }
}
